package com.elmcode.server.service.impl;

import com.elmcode.server.dao.EvaluateDao;
import com.elmcode.server.entity.BookEntity;
import com.elmcode.server.entity.CustomerEntity;
import com.elmcode.server.entity.EvaluateEntity;
import com.elmcode.server.service.CustomerService;
import com.elmcode.server.service.EvaluateService;
import com.elmcode.server.vo.EvaluateVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elmcode.common.utils.PageUtils;
import com.elmcode.common.utils.Query;


@Service("evaluateService")
public class EvaluateServiceImpl extends ServiceImpl<EvaluateDao, EvaluateEntity> implements EvaluateService {

    @Autowired
    CustomerService customerService;

    @Override
    public List<EvaluateVo> queryPage(Map<String, Object> params) {

        QueryWrapper<EvaluateEntity> queryWrapper = new QueryWrapper<>();

        //userId
        String userId = (String) params.get("userId");
        if(!StringUtils.isEmpty(userId)){
            queryWrapper.eq("user_id",userId);
        }

        String number = (String) params.get("num");
        if(!StringUtils.isEmpty(number)){
            queryWrapper.eq("order_no",number);
        }
        String storeName = (String) params.get("storeName");
        if(!StringUtils.isEmpty(storeName)){
            queryWrapper.like("store_name",storeName);
        }

        List<EvaluateEntity> entities = this.baseMapper.selectList(queryWrapper);

        List<EvaluateVo> collect = entities.stream().map(item -> {
            EvaluateVo evaluateVo = new EvaluateVo();
            BeanUtils.copyProperties(item, evaluateVo);

            CustomerEntity byId = customerService.getById(item.getCustomerId());
            evaluateVo.setCustomerName(byId.getName());

            return evaluateVo;
        }).collect(Collectors.toList());

        return collect;
    }

}